package com.ajieee.merge.controller.finance;

import com.ajieee.merge.entity.common.result.ApiResult;
import com.ajieee.merge.entity.common.result.PageDTO;
import com.ajieee.merge.entity.finance.dto.KlinePatternQuery;
import com.ajieee.merge.entity.finance.dto.KlinePatternReqDTO;
import com.ajieee.merge.entity.finance.po.KlinePatternPO;
import com.ajieee.merge.service.KlinePatternService;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDate;
import java.util.List;

/**
 * @author ajieee
 * @date 2025-11-01
 */
@RestController
@RequiredArgsConstructor
@RequestMapping("/kline/pattern")
public class KlinePatternController {

	private final KlinePatternService klinePatternService;

	/**
	 * 新增K线形态
	 */
	@PostMapping("/save")
	public ApiResult<Void> save(@Validated @RequestBody KlinePatternReqDTO dto) {
		klinePatternService.save(dto);
		return ApiResult.success();
	}

	/**
	 * 更新K线形态
	 */
	@PostMapping("/update")
	public ApiResult<Void> update(@Validated @RequestBody KlinePatternReqDTO dto) {
		klinePatternService.update(dto);
		return ApiResult.success();
	}

	/**
	 * K线形态列表
	 */
	@GetMapping("/list")
	public ApiResult<PageDTO<KlinePatternPO>> list(KlinePatternQuery query) {
		PageDTO<KlinePatternPO> page = klinePatternService.list(query);
		return ApiResult.success(page);
	}

	/**
	 * K线形态详情
	 */
	@GetMapping("/{id}")
	public ApiResult<KlinePatternPO> list(@PathVariable Long id) {
		KlinePatternPO detail = klinePatternService.queryById(id);
		return ApiResult.success(detail);
	}

	@GetMapping("/backtest")
	public ApiResult<List<LocalDate>> backtest(@RequestParam String targetCode,
											   @RequestParam String patternId) {
		List<LocalDate> dates = klinePatternService.backtest(targetCode, patternId);
		return ApiResult.success(dates);
	}

}
